##############################
# This script computes several histograms of returns.
#
# **Figure A13: Stock Returns Since Purchase and Returns Since Peak (Week Definition)**
# - `figures/hist_retsincepur_MAXC_peak_5update_sellsample.pdf`
# - `figures/hist_retsincepeak_MAXC_peak_5update_sellsample.pdf`
#
# **Figure A15: Stock Returns Since Purchase and Returns Since Peak (Month Definition)**
# - `figures/hist_retsincepur_MAXC_peak_20update_sellsample.pdf`
# - `figures/hist_retsincepeak_MAXC_peak_20update_sellsample.pdf`
#
# **Figure A17: Stock Returns Since Purchase and Returns Since Peak, Login-Day Sample**
# - `figures/hist_retsincepur_MAXC_peak_5update_loginsample.pdf`
# - `figures/hist_retsincepeak_MAXC_peak_5update_loginsample.pdf`
##############################

if (use_days==sell & data_name_read=="MAXC_peak_5update.csv"){

setwd(wd_figures)
pdf(paste0("hist_retsincepur_",  plot_name, "_", use_days ,"sample.pdf"), height=5, width=5, paper='special') 

hist(data_for_peaks$return.since.pur100, 
     breaks=100,
     main="", 
     freq=T,
     xlab="Return Since Purchase (%)",
     ylim=c(0, 30000), yaxt="n",
     xlim=c(-100, 100), family = "serif"
     ,cex=1.3, 
     cex.axis=1.3, cex.lab=1.3
)
axis(2, at=seq(0,30000,by=5000))

dev.off() 


pdf(paste0("hist_retsincepeak_",  plot_name, "_", use_days ,"sample.pdf"), height=5, width=5, paper='special') 

maxlim=max(data_for_peaks$return.since.point100)
hist(data_for_peaks$return.since.point100, 
     breaks=100,
     main="", 
     freq=T,
     xlab="Return Since Peak (%)",
     ylim=c(0, 20000), yaxt="n", 
     xlim=c(-100, maxlim*1.5), family = "serif", 
     cex=1.3, 
     cex.axis=1.3, cex.lab=1.3
)
axis(2, at=seq(0,20000,by=5000))

dev.off()

}
if (use_days==login & data_name_read=="MAXC_peak_5update.csv"){
  
  setwd(wd_figures)
  pdf(paste0("hist_retsincepur_",  plot_name, "_", use_days ,"sample.pdf"), height=5, width=5, paper='special') 
  
  hist(data_for_peaks$return.since.pur100, 
       breaks=100,
       main="", 
       freq=T,
       xlab="Return Since Purchase (%)",
       ylim=c(0, 500000), yaxt="n",
       xlim=c(-100, 100), family = "serif"
       ,cex=1.3, 
       cex.axis=1.3, cex.lab=1.3
  )
  axis(2, at=seq(0,500000,by=100000))
  
  dev.off() 
  
  
  pdf(paste0("hist_retsincepeak_",  plot_name, "_", use_days ,"sample.pdf"), height=5, width=5, paper='special') 
  
  maxlim=max(data_for_peaks$return.since.point100)
  hist(data_for_peaks$return.since.point100, 
       breaks=100,
       main="", 
       freq=T,
       xlab="Return Since Peak (%)",
       ylim=c(0, 400000), yaxt="n", 
       xlim=c(-100, maxlim*1.5), family = "serif", 
       cex=1.3, 
       cex.axis=1.3, cex.lab=1.3
  )
  axis(2, at=seq(0,400000,by=100000))
  
  dev.off()
  
}



if (use_days==sell & data_name_read=="MAXC_peak_20update.csv"){
  
  setwd(wd_figures)
  pdf(paste0("hist_retsincepur_",  plot_name, "_", use_days ,"sample.pdf"), height=5, width=5, paper='special') 
  
  hist(data_for_peaks$return.since.pur100, 
       breaks=100,
       main="", 
       freq=T,
       xlab="Return Since Purchase (%)",
       ylim=c(0, 20000), yaxt="n",
       xlim=c(-100, 100), family = "serif"
       ,cex=1.3, 
       cex.axis=1.3, cex.lab=1.3
  )
  axis(2, at=seq(0,20000,by=5000))
  
  dev.off() 
  
  
  pdf(paste0("hist_retsincepeak_",  plot_name, "_", use_days ,"sample.pdf"), height=5, width=5, paper='special') 
  
  maxlim=max(data_for_peaks$return.since.point100)
  hist(data_for_peaks$return.since.point100, 
       breaks=100,
       main="", 
       freq=T,
       xlab="Return Since Peak (%)",
       ylim=c(0, 12000), yaxt="n", 
       xlim=c(-100, maxlim*1.5), family = "serif", 
       cex=1.3, 
       cex.axis=1.3, cex.lab=1.3
  )
  axis(2, at=seq(0,12000,by=4000))
  
  dev.off()
  
}

if (use_days==login & plot_name=="MAXC_peak_20update"){
  
  setwd(wd_figures)
  pdf(paste0("hist_retsincepur_",  plot_name, "_", use_days ,"sample.pdf"), height=5, width=5, paper='special') 
  
  hist(data_for_peaks$return.since.pur100, 
       breaks=100,
       main="", 
       freq=T,
       xlab="Return Since Purchase (%)",
       ylim=c(0, 350000), yaxt="n",
       xlim=c(-100, 100), family = "serif"
       ,cex=1.3, 
       cex.axis=1.3, cex.lab=1.3
  )
  axis(2, at=seq(0,350000,by=50000))
  
  dev.off() 
  
  
  pdf(paste0("hist_retsincepeak_",  plot_name, "_", use_days ,"sample.pdf"), height=5, width=5, paper='special') 
  
  maxlim=max(data_for_peaks$return.since.point100)
  hist(data_for_peaks$return.since.point100, 
       breaks=100,
       main="", 
       freq=T,
       xlab="Return Since Peak (%)",
       ylim=c(0, 200000), yaxt="n", 
       xlim=c(-100, maxlim*1.5), family = "serif", 
       cex=1.3, 
       cex.axis=1.3, cex.lab=1.3
  )
  axis(2, at=seq(0,200000,by=50000))
  
  dev.off()
  
}










